<?php
require_once 'phpmoko/lib/common.php';

class Browser
{
	const RESPONSE_301_MOVED_PERMANENTLY = 301;
	const RESPONSE_302_FOUND = 302;
	const RESPONSE_404_NOT_FOUND = 404;
	const RESPONSE_410_GONE = 410;

	static function redirectToUrl($url, $response = Browser::RESPONSE_302_FOUND)
	{
		http_response_code($response);
		header("Location: " . $url);
		exit();
	}

	static function getSubdomain()
	{
		static $subdomain = null;
		if ($subdomain == null) {
			if (isset($_SERVER["HTTP_HOST"])) {

				$subdomain = explode(".", $_SERVER["HTTP_HOST"]);
				$size = sizeof($subdomain);
				if ($size >= 3)
					$subdomain = $subdomain[$size - 3];
				else
					$subdomain = "";
			} else
				$subdomain = "";
		}

		return $subdomain;
	}

	static function getDomain()
	{
		static $domain = null;
		if ($domain == null) {
			if (isset($_SERVER["HTTP_HOST"])) {
				$name = explode(".", $_SERVER["HTTP_HOST"]);
				$size = sizeof($name);
				$domain = $name[$size - 2] . "." . $name[$size - 1];
			} else
				$domain = "localhost";
		}

		return $domain;
	}

	static function getDirectory()
	{
		static $directory = null;
		if ($directory == null) {
			$directory = $_SERVER["REQUEST_URI"];
			$directory = substr($directory, 0, strrpos($directory, "/") + 1);
		}

		return $directory;
	}

	static function getFile()
	{
		static $file = null;
		if ($file == null) {
			$file = $_SERVER["REQUEST_URI"];
			$file = substr($file, strrpos($file, '/') + 1);
		}

		return $file;
	}

	/**
	 * Convierte una variable pasada por URL a una variable normal, en realidad
	 * lo que hace es quitar los magic quotes que se ponen por defecto, solo lo hace
	 * si realmente se añadieron por culpa del php
	 *
	 * @param
	 *        	Valor a traducir
	 * @return Valor traducido
	 */
	static function convertUrl2Var($value)
	{
		if (is_array($value)) {
			$new_value = array();
			foreach ($value as $key => $val)
				$new_value[$key] = Browser::convertUrl2Var($val);

			return $new_value;
		}

		if (get_magic_quotes_gpc())
			return stripslashes($value);

		return trim($value);
	}

	/**
	 * Search a parameter
	 *
	 * @param string $param
	 * @param int $type:
	 *        	1 GET (url), 2 POST (form), 4 Module (htaccess)
	 * @return string
	 */
	static function getUrlParameter($param, $type = -1)
	{
		if ($type & 0x4)
			if (isset($_GET[$param]))
				return trim($_GET[$param]);

		if ($type & 0x2)
			if (isset($_POST[$param]))
				return Browser::convertUrl2Var($_POST[$param]);

		if ($type & 0x1) {
			$arr = @parse_url($_SERVER["REQUEST_URI"]);
			if (isset($arr["query"])) {
				parse_str($arr["query"], $output);
				if (isset($output[$param])) {
					$content = $output[$param];
					if ($content)
						return Browser::convertUrl2Var($content);

					return null;
				}
			}
		}

		return FALSE;
	}

	static function isValidUrl($url)
	{
		if (filter_var($url, FILTER_VALIDATE_URL) === false)
			return false;
		return true;
	}

	static function getUserAgent()
	{
		return $_SERVER['HTTP_USER_AGENT'];
	}
}
?>